﻿/**
 * 表格插件
 * 请注意插件会继承 Base_Plugin 的属性，在core.js当中定义
 */
(function(E, $){

var Consts = E.consts,
	timeid=0,
	tableIndex=0,
	tables = {},
	Menu = E.mod('Menu'),
	Event = E.mod('Event'),
	isHudong = false;

Event.bind("oncomplete", function(){
	E.plugin("InsertTable").setTableId();
});

// 插入表格
E.plugin("InsertTable", {
	menuType:'Menu',
	menuTitle: '表格操作',
	isNeedTitle:0,
	
	//依赖那些模块
	//对于核心模块，不需要检查，主要用来检查自定义模块
	depends: ['Panel', 'Selection'],
	
	config: {
		//面板宽度
		width:280
	},
	//配置信息
	settings : {
		isHudong : false
	},
	
	// 在插件初始化时执行
	init: function( self ){
		var html=['<table id="jqe-plugin-table" class="jqe-plugin-table">'];
		for(var i=1; i<=10; i++){
			html.push('<tr>');
			for(var j=1; j<=12; j++){
				html.push('<td ij="'+i+'-'+j+'"><a href="jqe:点击插入一个'+i+'行'+j+'列的表格" title="'+i+'行'+j+'列, 点击插入" onclick="return false;">&nbsp;</a></td>');
			}
			html.push('</tr>');
		}
		html.push('<tr><td colspan="12" style="text-align:left;color:#666666;width:100%;line-height:18px;">');
		html.push('&nbsp;表格宽 <input name="width" style="width:40px;height:18px;line-height:18px;" maxlength="4" value=""/>');

		
		html.push('<span class="jqe-plugin-table-xtrxtd">x行x列</span></td></tr>');
		html.push('</table>');
		
		self.cache['html'] = html.join('');
		//记录是否是互动
		if(self.settings.isHudong){
			isHudong = true;
		}
	},
	
	setTableId: function(){
		E.$('table').each(function(i, el){
			if( !el.id ){
				el.id = "jqe-table-"+tableIndex;
				tableIndex += 1;
			}
			
			// 扫描表格
			Table.scanTable(el);
		});
	},
	
	// 在面板第一次打开时执行一次
	one: function( panel, self ){
		var self = this, tds = panel.find('.jqe-plugin-table td[ij]');
		tds.mouseover(function(){
			var o = $(this) ;
			clearTimeout(timeid);
			// 通过使用setTimeout来避免onmouseover事件的大量执行
			timeid = setTimeout(function(){
				self.hightLight( o, tds, panel );
			}, 100);
		});
	},
	
	hightLight: function(o, tds, panel){
		var  ij=o.attr("ij").split('-'), ix=ij[0], jx=ij[1];
		tds.removeClass('hover');
		
		for(var i=1; i<=ix; i++){
			for(var j=1; j<=jx; j++){
				tds.filter('[ij="'+i+'-'+j+'"]').addClass('hover');
			}
		}
		
		panel.find('.jqe-plugin-table-xtrxtd').text(ix+'行 '+jx+'列');
	},
	
	/**
	 * 点击面板时会触发此方法
	 * 插入表格
	 */
	action: function( target, panel, self ){
		var name = self.name,
			ij = target.closest('[ij]'),
			oWidth = panel.find('input[name=width]'),
			width = $.trim(oWidth.val());
		if( !ij.size() ){
			return false;
		}
		
		if( !(width && /^(\d{0,4}|\d{1,3}%)$/.test(width)) ){
			width = '';
		}
		
		if(width){
			oWidth.val(width);
		}
		
		ij = ij.attr('ij').split('-'), ix=ij[0], jx=ij[1];
		self.hide();
		var html=['<table class="jqe-table" width="'+width+'" border="1" bordercolor="#cccccc">'];
		if(isHudong){
			html = ['<table class="table" width="'+width+'">'];
		}
		html.push('<tbody>');
		//互动词条默认不加表头
		if(!isHudong){
			html.push('<tr>');
			for(var j=1; j<=jx; j++){
				html.push('<th>&nbsp;&nbsp;</th>');//&#8203;<br>
			}
		}
		html.push('</tr>');
		for(var i=1; i<=ix; i++){
			html.push('<tr>');
			for(var j=1; j<=jx; j++){
				html.push('<td>&nbsp;&nbsp;</td>');
			}
			html.push('</tr>');
		}
		html.push('</tbody></table></div>');
		
		setTimeout(function(){E.plugin("InsertTable").setTableId();}, 0);
		
		return html.join('');
	},
	
	menu: function( Panel, self, target ){
		var Menu = E.mod('Menu');
		
		Menu.set( self.event, self.name );
		return 1;
	}
});


//添加工具栏表格菜单，有选中内容时触发
Menu.add('InsertTable', {
	title: '表格下拉菜单',
	
	check: function( cursorElement, e ){
		return true;
	},
	
	getHtml: function( cursorElement ){
		var element = Event.getElement(),
			plugins = null, table, cell, th, caption;
			
		if(element){
			table = element.table;
			cell = element.cell;
		}
		
		if( table ){
			th = table.getElementsByTagName('th');
			caption = table.caption;
			
			plugins = {
				InsertTable: 0,
				addTableTitleRow : 1,
				RemoveTableTitleRow : 1,
				
				addTableCaption : 1,
				RemoveTableCaption : 1,

				Insert : {
					RowInsertBefore : 1,
					RowInsertAfter : 1,
					
					ColumnInsertBefore : 1,
					ColumnInsertAfter : 1,
					Append5Row : 1,
					Append5Column : 1
				},
				Remove : {
					RemoveRow : 1,
					RemoveColumn : 1,
					
					EmptyTable : 1,
					RemoveTable : 1
				},
				MergeCell:{
					ColumnMergeAfter : 1,
					RowMergeAfter : 1,
					CancelCellMerge : 1
				},
				'-a':1,
				SetTableWidth : {
					TableWidthDefault : 1,
					TableWidthAuto : 1,
					TableWidthFixed : 1,
					TableWidthAverage : 1
				},
				
				SetCellWidth : {
					SetCellWidthAdd10 : 1,
					SetCellWidthReduce10 : 1,
					SetCellWidthAdd50 : 1,
					SetCellWidthReduce50 : 1
				},
				SetTableAlign : {
					SetTableAlignLeft : 1,
					SetTableAlignRight : 1,
					SetTableAlignCenter : 1
				},
				SetColumnAlign : {
					SetColumnAlignLeft : 1,
					SetColumnAlignRight : 1,
					SetColumnAlignCenter : 1,
					SetColumnValignTop : 1,
					SetColumnValignCenter: 1
				},
				SetRowAlign : {
					SetRowAlignLeft: 1,
					SetRowAlignRight: 1,
					SetRowAlignCenter: 1,
					SetRowValignTop: 1,
					SetRowValignCenter: 1
				},
				SetCellAlign : {
					SetCellAlignLeft: 1,
					SetCellAlignRight: 1,
					SetCellAlignCenter: 1,
					SetCellValignTop: 1,
					SetCellValignCenter: 1
				},
				TableMove:{
					RowMoveBefore: 1,
					RowMoveAfter: 1,
					RowMoveFirst: 1,
					RowMoveLast: 1
				}
			}
			
			
			if( caption && caption.innerHTML ){
				delete plugins.addTableCaption;
			}else{
				delete plugins.RemoveTableCaption;
			}
			
			if( th.length ){
				delete plugins.addTableTitleRow;
			}else{
				delete plugins.RemoveTableTitleRow;
			}
			if( cell.rowSpan == 1 && cell.colSpan == 1){
				plugins.MergeCell.CancelCellMerge = 0;
			}

		}else{
			// 光标不在表格内，插入表格菜单项可用，其他表格操作均不可用
			plugins = {
				InsertTable: element?2:0,
				addTableTitleRow : 0,
				RemoveTableTitleRow : 0,
				
				addTableCaption : 0,
				RemoveTableCaption : 0,

				Insert : 0,
				Remove : 0,
				MergeCell:0,
				'-a':1,
				SetTableWidth : 0,
				
				SetCellWidth : 0,
				SetTableAlign : 0,
				SetColumnAlign : 0,
				SetRowAlign : 0,
				SetCellAlign : 0,
				TableMove : 0
			}
		}
		//如果是互动的话不用表格标题行操作
		if(isHudong){
			delete plugins.addTableTitleRow;
			delete plugins.RemoveTableTitleRow;
		}
		return plugins;
	}
	
});


//添加一个右键菜单，没有选中内容时触发
Menu.add('contextmenu', {
	title: '一个右键菜单，没有选中内容时触发',
	
	check: function( cursorElement, e ){
		if( cursorElement && typeof cursorElement == 'object'){
			if( /^(h2|h3|img|a|sup)$/i.test(cursorElement.nodeName)
				|| e.ctrlKey
			){
				return false;
			}else if( /^(td|th|caption)$/i.test(cursorElement.nodeName)
				|| E.$(cursorElement).closest('tr').size()
			){
				return true;
			}
		}else{
			return false;
		}
	},
	
	getHtml: function( cursorElement ){
		var plugins = {Redo:1, Undo:1},
			cell = E.$(cursorElement).closest('td,th'),
			row,table,
			a = E.$(cursorElement).closest('a');
		
		if( cell.size() ){
			row = cell.closest('tr');
			table = row.closest('table');
			cell = cell[0];
			row = row[0];
			table = table[0];			
			th = table.getElementsByTagName('th');
			
			plugins = {
				addTableTitleRow : 1,
				RemoveTableTitleRow : 1,
				
				addTableCaption : 1,
				RemoveTableCaption : 1,

				Insert : {
					RowInsertBefore : 1,
					RowInsertAfter : 1,
					
					ColumnInsertBefore : 1,
					ColumnInsertAfter : 1,
					Append5Row : 1,
					Append5Column : 1
					
				},
				Remove : {
					RemoveRow : 1,
					RemoveColumn : 1,
					
					EmptyTable : 1,
					RemoveTable : 1
				},
				MergeCell:{
					ColumnMergeAfter : 1,
					RowMergeAfter : 1,
					CancelCellMerge : 1
				},
				'-a':1,
				SetTableWidth : {
					TableWidthDefault : 1,
					TableWidthAuto : 1,
					TableWidthFixed : 1,
					TableWidthAverage : 1
				},
				
				SetCellWidth : {
					SetCellWidthAdd10 : 1,
					SetCellWidthReduce10 : 1,
					SetCellWidthAdd50 : 1,
					SetCellWidthReduce50 : 1
				},
				SetTableAlign : {
					SetTableAlignLeft : 1,
					SetTableAlignRight : 1,
					SetTableAlignCenter : 1
				},
				SetColumnAlign : {
					SetColumnAlignLeft : 1,
					SetColumnAlignRight : 1,
					SetColumnAlignCenter : 1,
					SetColumnValignTop : 1,
					SetColumnValignCenter: 1
				},
				SetRowAlign : {
					SetRowAlignLeft: 1,
					SetRowAlignRight: 1,
					SetRowAlignCenter: 1,
					SetRowValignTop: 1,
					SetRowValignCenter: 1
				},
				SetCellAlign : {
					SetCellAlignLeft: 1,
					SetCellAlignRight: 1,
					SetCellAlignCenter: 1,
					SetCellValignTop: 1,
					SetCellValignCenter: 1
				},
				TableMove:{
					RowMoveBefore: 1,
					RowMoveAfter: 1,
					RowMoveFirst: 1,
					RowMoveLast: 1
				}
			}
			
			caption = table.caption;
			if( caption && caption.innerHTML ){
				delete plugins.addTableCaption;
			}else{
				delete plugins.RemoveTableCaption;
			}
			
			if( th.length ){
				delete plugins.addTableTitleRow;
			}else{
				delete plugins.RemoveTableTitleRow;
			}
			
			if( cell.rowSpan == 1 && cell.colSpan == 1){
				plugins.MergeCell.CancelCellMerge = 0;
			}
		}else{
			plugins.Image = 1;
			plugins.InsertTable = 2;
			if( a.size() ){
				plugins.Link = 1;
			}
			
			plugins.Source = 1;
		}
		//如果是互动的话不用表格标题行操作
		if(isHudong){
			delete plugins.addTableTitleRow;
			delete plugins.RemoveTableTitleRow;
		}
		return plugins;
	}
	
});



// 删除表格
E.plugin("RemoveTable", {
	isNeedPanel: 0,
	
	click: function( Menu, self, target ){
		var table = Event.getElement().table;
		E.$(table).remove();
		return true;
	}
});

// 删除一行
E.plugin("RemoveRow", {
	isNeedPanel: 0,
	
	click: function( Menu, self, target ){
		var row = Event.getElement().row,
			table = E.$(row).closest('table');
		E.$(row).remove();
		
		if(table.size() ){
			Table.scanTable(table[0]);
		}
		return true;
	}
});

// 表格的各种操作
var Table = {
	// 删除列
	removeColumn: function( Menu ){
		var self = this, cellIndex, table, eles;
		
		eles = Event.getElement();
		table = eles.table;
		cellIndex = eles.cell.cellIndex;
		
		for (var i = 0, len = table.rows.length; i < len; i++) {
			table.rows[i].deleteCell(cellIndex);
		}
		
		// 重新扫描表格
		self.scanTable(table);
	},
	
	// 插入行
	// 当存在合并的单元格时，此操作可能导致表格乱掉，尚待解决...
	// 已经初步解决 2012-01-21 潘雪鹏
	insertRow: function( table, row, offset ){
		var self = this, newRow, cloneRow, rowIndex;
		// offset = 0 上面插入行
		// offset = 1 下面插入行
		offset = offset || 0;
		if(table && row){
			rowIndex = row.rowIndex;
			// 当前行存在行合并的单元格时，
			// 向下插入行，
			// 改变为在下一行向上插入行
			if( offset ){
				var cells = row.cells;
				for(var i=0, len=cells.length; i<len; i++){
					if( cells[i].rowSpan > 1 ){
						offset = 0;
						rowIndex += 1;
						row = table.rows[rowIndex];
						break;
					}
				}
			}
			
			cloneRow = E.$(row).clone();
			cloneRow.children().html('&nbsp;').attr('rowSpan',1);
			newRow = table.insertRow(rowIndex + offset);
			
			// 如果当前行存在被垂直合并的单元格，
			// 则将合并当前行的单元格rowSpan属性加1
			var o = tables[table.id];
			if( o[rowIndex] ){
				for( var i in o[rowIndex] ){
					o[rowIndex][i].rowSpan += 1; 
				}
			}
			
			// 在IE下出现运行时错误
			// newRow.innerHTML = cloneRow.html();
			
			// 原理和innerHTML一样，这样可以
			E.$(newRow).html( cloneRow.html() );
			
			// 重新扫描表格
			self.scanTable(table);
		}
	},
	
	// 检查某行的下属单元格是否有跨行属性
	isRowspan: function( row ){
		var bool = false, cells = row.cells;
		
		for(var i=0, len=cells.length; i<len; i++){
			if( cells[i].rowSpan > 1 ) {
				bool = true;
				break;
			}
		}
		return bool;
	},
	
	// 上下移动行
	moveRow: function(table, row, flag){
		flag = flag || 'after';
		var self=this, prev, next;
		if( table && row ){
			if( flag === 'before' ) {
			// 向上移动
				prev = E.$(row).prev();
				if( prev.size() ){
					if( prev.children().size() !== row.cells.length ){
						E.mod("Message").showError('提示：上下行单元格数不相等，不能上下移动。');
					}else if( self.isRowspan(prev[0]) || self.isRowspan(row) ){
					//}else if( prev.children('[rowspan]').size() || E.$(row).children('[rowspan]').size() ){
						// 2012-02-06 潘雪鹏
						// IE下根据td属性[rowspan]会匹配到那些看上去没有这个属性的单元格
						// 所以逐个单元格检查比较好一些
						
						E.mod("Message").showError('提示：上下行单元格有跨行合并时，不能上下移动。');
					}else{
						prev.before( row );
					}
				}
			}else if( flag === 'after' ){
			// 向下移动
				next = E.$(row).next();
				if( next.size() ){
					if( next.children().size() !== row.cells.length ){
						E.mod("Message").showError('提示：上下行单元格数不相等，不能上下移动。');
					//}else if( next.children('[rowspan]').size() || E.$(row).children('[rowspan]').size() ){
					}else if( self.isRowspan(next[0]) || self.isRowspan(row) ){
						E.mod("Message").showError('提示：上下行单元格有跨行合并时，不能上下移动。');
					}else{
						next.after( row );
					}
				}
			}else if( flag === 'first' ){
			// 顶部
				E.$(table).prepend(row);
			}else if( flag === 'last' ){
			// 底部
				E.$(table).append(row);
			}
			
			self.scanTable(table);
		}
	},
	
	// 添加表格标题行
	addTitleRow: function( table ){
		var titleRow, html='', cloneRow;
		if(table){
			cloneRow = E.$(table.rows[0]).clone();
			//.removeAttr('rowSpan')  在IE8下面程序自动执行时出错
			//cloneRow.children().html('&nbsp;').removeAttr('rowSpan');
			cloneRow.children().html('&nbsp;').attr('rowSpan',1);
			html = cloneRow.html().replace(/td/ig, 'th');
			
			titleRow = table.insertRow(0);
			// 在IE下出现运行时错误
			// titleRow.innerHTML = html;
			
			 E.$(titleRow).html(html);
		}
	},
	
	// 插入列
	// 注意有标题行时，插入列需要处理标题行里面的列使用th标签，默认是td标签
	insertColumn: function( table, cell, offset ){
		var self = this, newCell, cellIndex, row, cloneCell, oldcellIndex;
		oldcellIndex = self.getCellIndex(table, cell);
		cellIndex = oldcellIndex + offset;
		
		for (var i = 0, len = table.rows.length; i < len; i++) {
			row = table.rows[i];
			if( cellIndex <= row.cells.length ){
				cloneCell = E.$(row.cells[oldcellIndex]).clone();
				cloneCell[0].colSpan = 1;
				cloneCell[0].rowSpan = 1;
				cloneCell.html('&nbsp;');
				newCell = row.insertCell(cellIndex);
				E.$(newCell).replaceWith( cloneCell[0] );
			}
		}
		self.scanTable(table);
	},
	
	setCell: function( Menu ){
		var table, eles, cell;
		
		eles = Event.getElement();
		if(eles.cell){
			cell = eles.cell;
		}
	},
	
	setTable: function( Menu ){
		
	},
	
	// 设置表格名称
	setCaption: function( Menu ){
		var table, eles;
		
		eles = Event.getElement();
		table = eles.table;
		if(table.caption){
			table.deleteCaption();
		}else{
			var t = table.createCaption();
			t.innerHTML = '表格名称';
		}
	},
	
	// 向右合并单元格
	mergeColumn: function(Menu, offset){
		var self=this, eles, cell, next, row, prev, colSpan, cellIndex;
		
		eles = Event.getElement();
		cell = eles.cell;
		row = eles.row;
		colSpan = cell.colSpan;
		cellIndex = cell.cellIndex;
		
		if(cellIndex < row.cells.length -1){
		// 向右合并
			next = row.cells[cellIndex+1];
			if( next && cell.rowSpan == next.rowSpan ){
			// 如果右侧的单元格存在，并且他们的行跨度相同
			// 行跨度不同的话不能合并
				cell.colSpan = colSpan + next.colSpan;
				
				//将内容合并
				cell.innerHTML += next.innerHTML;
				// 删除下面的单元格
				row.removeChild(next);
			}
			
			self.scanTable(eles.table);
		}
	},
	
	// 向下合并单元格
	// 需要考虑下单元格是否存在列跨度
	mergeRow: function(Menu, offset){
		var self = this, eles, cell, next, table, cellIndex, rowIndex, rowSpan, colSpan, row, nextRow;
	
		eles = Event.getElement();
		table = eles.table;
		cell = eles.cell;
		row = eles.row;
		cellIndex = cell.cellIndex;
		rowIndex = row.rowIndex;
		rowSpan = cell.rowSpan;
		colSpan = cell.colSpan;
		
		if( rowIndex < table.rows.length - 1 ){
		// 向下合并
			nextRow = table.rows[rowIndex + rowSpan];
			next = nextRow.cells[cellIndex];
			
			if( next && cell.colSpan == next.colSpan ){
				if( row.cells.length > 1 ){
					cell.rowSpan = rowSpan + next.rowSpan;
				}
				cell.innerHTML += next.innerHTML;
				nextRow.removeChild(next);
				
				if(!nextRow.cells.length){
					table.deleteRow(nextRow.rowIndex);
				}
			}
			self.scanTable(table);
		}
	},
	
	// 取消单元格合并
	unMerge: function( table, row, cell ){
		var self = this, rowSpan, colSpan, cellIndex=cell.cellIndex, newCell, rowIndex = row.rowIndex;
		if( cell.rowSpan > 1 ){
			// 取消纵向合并
			rowSpan = cell.rowSpan;
			cell.rowSpan = 1;
			
			for(var i=0; i<rowSpan-1; i++){
				newCell = table.rows[ rowIndex + i +1 ].insertCell(cellIndex);
				newCell.innerHTML = '&nbsp;';
			}
		}else if( cell.colSpan > 1 ){
			//取消横向合并
			colSpan = cell.colSpan;
			cell.colSpan = 1;
			for(var i=0; i<colSpan-1; i++){
				newCell = row.insertCell(cellIndex+1);
				newCell.innerHTML = '&nbsp;';
			}
		}
		
		self.scanTable(table);
	},
	
	// 表格扫描
	// 目的：找出那些被合并的单元格
	// 在表格发生变化时，需要重新扫描
	scanTable: function( table ){
		var id = table.id,
			colsLength = 0,
			rowsLength = table.rows.length,
			rows = table.rows;
			cells = rows[0].cells;

			// 得到表格的列数
		for(var i=0, len = cells.length; i<len; i++){
			colsLength += cells[i].colSpan;
		}

		// 找出那些被合并的单元格
		var arr = {};
		for(var i=0, len = rows.length; i<len; i++){
			for(var j=0, len2 = rows[i].cells.length; j<len2; j++){
				var cell = rows[i].cells[j];
				if( cell.colSpan > 1 && cell.rowSpan > 1 ){
					// 同时存在行合并和列合并，暂时忽略
					// todo ...
				}else if(cell.colSpan > 1){
					// 跨列
					arr[i] = {};
					for(var index=j+1, len3 = j+cell.colSpan; index < len3; index++ ){
						arr[i][index] = cell;
					}
				}else if(cell.rowSpan > 1){
					// 跨行
					for(var index = i+1, len3 = i+cell.rowSpan; index<len3; index++){
						arr[index] = {};
						arr[index][j] = cell;
					}
				}
			}
		}
		
		tables[table.id] = arr;
		return arr;
	},
	
	// 检查单元格是否存在
	isExistCell: function(table, rowIndex, cellIndex){
		var bool = true, id = table.id, o;
		if( !id ){
			table.id = "jqe-table-"+tableIndex;
			o = Table.scanTable(table);
		}else{
			o = tables[id];
		}
		
		if( o[rowIndex] && o[rowIndex][cellIndex] ){
			bool = false;
		}
		
		return bool;
	},
	
	// 获取单元格在有合并时的真实索引
	getCellIndex: function(table, cell){
		var self = this,
			cellIndex = cell.cellIndex,
			id = table.id,
			row = cell.parentNode,
			rowIndex = row.rowIndex,
			o = tables[id];
		
		if( o[rowIndex] ){
			for( var i in o[rowIndex] ){
				if( i <= cellIndex ){
					cellIndex +=1;
				}
			}
		}
		return cellIndex;
	},
	
	// 根据索引获取单元格
	getCellByIndex: function(table, row, index){
		var self = this,
			cell = null, 
			id = table.id,
			rowIndex = row.rowIndex,
			o = tables[id];
		
		if( o[rowIndex] ){
			for( var i in o[rowIndex] ){
				if( i <= index ){
					index -=1;
				}
			}
		}
		
		index = index > 0 ? index : 0;
		return row.cells[index];
	},
	
	// 由于单元格可以合并，简单根据索引值直接操作，非常不准确
	// 列操作需要对整个表格进行扫描
	
	// 设置列宽度
	/*
	setColumnWidth: function( table, cell, offsetWidth){
		var self = this,
			width = E.$(cell).width() + offsetWidth,
			id = table.id,
			
			cellIndex = cell.cellIndex;
		
		
		for (var i = 0, len = table.rows.length; i < len; i++) {
			cell = table.rows[i].cells[cellIndex];
			
			if( cell && cell.colSpan == 1 && self.isExistCell(table, i, cellIndex) ){
				cell.width = width;
			}
		}
	},
	*/
	
	
	// 设置单元格宽度
	setCellWidth: function( table, cell, offsetWidth){
		var width = E.$(cell).width() + offsetWidth;
		
		if( cell && width>0 ){
			cell.width = width;
		}
	},
	
	// 设置单元格齐方式
	SetCellAlign: function( cell, align, vAlign){
		if( cell ){
			if(align)cell.align = align;
			if(vAlign)cell.vAlign = vAlign;
			// 注意 cell.valign = 100; 这样的设置无效，必须是 vAlign
		}
	},
	
	// 设置列对齐方式
	SetColumnAlign: function( table, cell, align, vAlign){
		var self=this, cellIndex = self.getCellIndex(table, cell);
		
		for (var i = 0, len = table.rows.length; i < len; i++) {
			//cell = table.rows[i].cells[cellIndex];
			cell = self.getCellByIndex(table, table.rows[i], cellIndex);
			
			if( cell && self.isExistCell(table, i, cellIndex)){
				if(align)cell.align = align;
				if(vAlign)cell.vAlign = vAlign;
			}
		}
	},
	
	// 设置行对齐方式
	SetRowAlign: function( row, align, vAlign){
		for (var i = 0, len = row.cells.length; i < len; i++) {
			cell = row.cells[i];
			if( cell ){
				if(align)cell.align = align;
				if(vAlign)cell.vAlign = vAlign;
			}
		}
	},
	
	// 设置表格整体对齐方式
	SetTableAlign: function( table, align){
		E.$(table).css({'marginLeft':'', 'marginRight':''});
		if( align === 'left' ) E.$(table).css('marginLeft', '5px');
		else if( align === 'right' ) E.$(table).css('marginRight', '5px');
	}
};

// 注意插件的click方法在执行成功时返回true，失败时返回false
// 表格插件仅需要返回true，不需要失败返回

// 删除一列
E.plugin("RemoveColumn", {
	click: function( Menu, self, target ){
		Table.removeColumn( Menu );
		return true;
	}
});

// 向上插入行
E.plugin("RowInsertBefore", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.insertRow( eles.table, eles.row, 0 );
		return true;
	}
});

// 向下插入行
E.plugin("RowInsertAfter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(),
			rowSpan = eles.cell.rowSpan;
		Table.insertRow( eles.table, eles.row, rowSpan );
		return true;
	}
});

// 行尾追加5行
E.plugin("Append5Row", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(), 
			rows = eles.table.rows,
			row = rows[rows.length-1];
		
		for(var i=0; i<5; i++){
			Table.insertRow( eles.table, row, 1 );
		}
		return true;
	}
});

// 向左插入列
E.plugin("ColumnInsertBefore", {
	click: function( Menu, self, target ){
		eles = Event.getElement();
		table = eles.table;
		cell = eles.cell;
			
		Table.insertColumn( table, cell, 0 );
		return true;
	}
});

// 向右插入列
E.plugin("ColumnInsertAfter", {
	click: function( Menu, self, target ){
		eles = Event.getElement();
		table = eles.table;
		cell = eles.cell;
			
		Table.insertColumn( table, cell, cell.colSpan );
		return true;
	}
});

// 列尾追加5列
E.plugin("Append5Column", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(),
			table = eles.table,
			rows = table.rows,
			width = eles.cell.width;
			ths = E.$('<th></th><th></th><th></th><th></th><th></th>'),
			tds = E.$('<td></td><td></td><td></td><td></td><td></td>');
		ths.html('&nbsp;');
		tds.html('&nbsp;');
		if(width){
			ths.width(width);
			tds.width(width);
		}
		
		for(var i=0, len=rows.length; i<len; i++){
			$.nodeName(rows[i].cells[0], 'th')
				? ths.clone().appendTo(rows[i])
				: tds.clone().appendTo(rows[i]);
		}
		return true;
	}
});

// 向右合并单元格
E.plugin("ColumnMergeAfter", {
	click: function( Menu, self, target ){
		Table.mergeColumn( Menu, 1 );
		return true;
	}
});

// 向下合并单元格
E.plugin("RowMergeAfter", {
	click: function( Menu, self, target ){
		Table.mergeRow( Menu, 1 );
		return true;
	}
});

// 取消单元格合并
E.plugin("CancelCellMerge", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.unMerge( eles.table, eles.row, eles.cell );
		return true;
	}
});

// 添加表格名称
E.plugin("addTableCaption", {
	click: function( Menu, self, target ){
		Table.setCaption( Menu);
		return true;
	}
});

// 删除表格名称
E.plugin("RemoveTableCaption", {
	click: function( Menu, self, target ){
		Table.setCaption( Menu);
		return true;
	}
});

// 添加表格标题行
E.plugin("addTableTitleRow", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		if( eles.table ){
			Table.addTitleRow( eles.table);
		}
		return true;
	}
});


// 删除表格标题行
E.plugin("RemoveTableTitleRow", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		if( eles.table ){
			eles.table.deleteRow( 0 );
		}
		return true;
	}
});

// 清空表格内容
E.plugin("EmptyTable", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(), table, row;
		if( eles.table ){
			table = eles.table;
			
			for (var i = 0, len = table.rows.length; i < len; i++) {
				row = table.rows[i];
				for (var j = 0, len2 = row.cells.length; j < len2; j++) {
					row.cells[j].innerHTML = '<br />';
				}
			}
		}
		return true;
	}
});

// 表格宽度 - 按表格文字自适应
E.plugin("TableWidthDefault", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(), table = eles.table, row;
		table.removeAttribute('width');
		for (var i = 0, len = table.rows.length; i < len; i++) {
			row = table.rows[i];
			for (var j = 0, len2 = row.cells.length; j < len2; j++) {
				row.cells[j].removeAttribute('width');
			}
		}
		return true;
	}
});

// 表格宽度 - 按页面宽度自适应
E.plugin("TableWidthAuto", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(), table = eles.table, row;
		table.width = '99%';
		for (var i = 0, len = table.rows.length; i < len; i++) {
			row = table.rows[i];
			for (var j = 0, len2 = row.cells.length; j < len2; j++) {
				row.cells[j].removeAttribute('width');
			}
		}
		return true;
	}
});

// 表格宽度 - 固定为当前列宽
E.plugin("TableWidthFixed", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(), table = eles.table, row, cell;
		width = E.$(table).width();
		table.removeAttribute('width');
		for (var i = 0, len = table.rows.length; i < len; i++) {
			row = table.rows[i];
			for (var j = 0, len2 = row.cells.length; j < len2; j++) {
				cell = row.cells[j];
				cell.width = E.$( cell ).outerWidth();
			}
		}
		return true;
	}
});

// 表格宽度 - 平均分布列宽
E.plugin("TableWidthAverage", {
	click: function( Menu, self, target ){
		var eles = Event.getElement(), table = eles.table, row, cell, width,
			cells = table.rows[0].cells, cellLength=0;
		
		for(var i=0, len=cells.length; i< len; i++){
			cellLength += cells[i].colSpan;
		}
		
		if(table.width){
			width = table.width / cellLength;
		}else{
			width = E.$(table).width() / cellLength;
		}
		
		width = Math.round(width)-10;
		
		table.removeAttribute('width');
		for (var i = 0, len = table.rows.length; i < len; i++) {
			row = table.rows[i];
			for (var j = 0, len2 = row.cells.length; j < len2; j++) {
				cell = row.cells[j];
				if(cell.colSpan == 1) {
					cell.width = width;
				}
			}
		}
		return true;
	}
});

/*
// 调整当前列宽度 - 增加10
E.plugin("SetColumnWidthAdd10", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setColumnWidth(eles.table, eles.cell, 10);
		return true;
	}
});

// 调整当前列宽度 - 减少10
E.plugin("SetColumnWidthReduce10", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setColumnWidth(eles.table, eles.cell, -10);
		return true;
	}
});

// 调整当前列宽度 - 增加50
E.plugin("SetColumnWidthAdd50", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setColumnWidth(eles.table, eles.cell, 50);
		return true;
	}
});

// 调整当前列宽度 - 减少50
E.plugin("SetColumnWidthReduce50", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setColumnWidth(eles.table, eles.cell, -50);
		return true;
	}
});
*/

// 调整当前单元格宽度 - 增加10
E.plugin("SetCellWidthAdd10", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setCellWidth(eles.table, eles.cell, 10);
		return false;
	}
});

// 调整当前单元格宽度 - 减少10
E.plugin("SetCellWidthReduce10", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setCellWidth(eles.table, eles.cell, -10);
		return false;
	}
});

// 调整当前单元格宽度 - 增加50
E.plugin("SetCellWidthAdd50", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setCellWidth(eles.table, eles.cell, 50);
		return false;
	}
});

// 调整当前单元格宽度 - 减少50
E.plugin("SetCellWidthReduce50", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.setCellWidth(eles.table, eles.cell, -50);
		return false;
	}
});


// 整列对齐方式 - 左对齐
E.plugin("SetColumnAlignLeft", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetColumnAlign(eles.table, eles.cell, 'left', '');
		return true;
	}
});

// 整列对齐方式 - 右对齐
E.plugin("SetColumnAlignRight", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetColumnAlign(eles.table, eles.cell, 'right', '');
		return true;
	}
});

// 整列对齐方式 - 水平居中
E.plugin("SetColumnAlignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetColumnAlign(eles.table, eles.cell, 'center', '');
		return true;
	}
});

// 整列对齐方式 - 垂直局顶
E.plugin("SetColumnValignTop", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetColumnAlign(eles.table, eles.cell, '', 'top');
		return true;
	}
});

// 整列对齐方式 - 垂直局中
E.plugin("SetColumnValignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetColumnAlign(eles.table, eles.cell, '', 'center');
		return true;
	}
});


// 整行对齐方式 - 左对齐
E.plugin("SetRowAlignLeft", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetRowAlign(eles.row, 'left', '');
		return true;
	}
});

// 整行对齐方式 - 右对齐
E.plugin("SetRowAlignRight", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetRowAlign(eles.row, 'right', '');
		return true;
	}
});

// 整行对齐方式 - 水平居中
E.plugin("SetRowAlignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetRowAlign(eles.row, 'center', '');
		return true;
	}
});

// 整行对齐方式 - 垂直局顶
E.plugin("SetRowValignTop", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetRowAlign(eles.row, '', 'top');
		return true;
	}
});

// 整行对齐方式 - 垂直局中
E.plugin("SetRowValignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetRowAlign(eles.row, '', 'center');
		return true;
	}
});


// 单元格对齐方式 - 左对齐
E.plugin("SetCellAlignLeft", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetCellAlign(eles.cell, 'left', '');
		return true;
	}
});

// 单元格对齐方式 - 右对齐
E.plugin("SetCellAlignRight", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetCellAlign(eles.cell, 'right', '');
		return true;
	}
});

// 单元格对齐方式 - 水平居中
E.plugin("SetCellAlignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetCellAlign(eles.cell, 'center', '');
		return true;
	}
});

// 表格对齐方式 - 左对齐
E.plugin("SetTableAlignLeft", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetTableAlign(eles.table, 'left');
		return true;
	}
});

// 单元格对齐方式 - 右对齐
E.plugin("SetTableAlignRight", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetTableAlign(eles.table, 'right');
		return true;
	}
});

// 单元格对齐方式 - 水平居中
E.plugin("SetTableAlignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetTableAlign(eles.table, 'center');
		return true;
	}
});

// 单元格对齐方式 - 垂直局顶
E.plugin("SetCellValignTop", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetCellAlign(eles.cell, '', 'top');
		return true;
	}
});

// 单元格对齐方式 - 垂直局中
E.plugin("SetCellValignCenter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.SetCellAlign(eles.cell, '', 'center');
		return true;
	}
});

// 向上移动一行
E.plugin("RowMoveBefore", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.moveRow(eles.table, eles.row, 'before');
		return true;
	}
});

// 向下移动一行
E.plugin("RowMoveAfter", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.moveRow(eles.table, eles.row, 'after');
		return true;
	}
});

// 移动到最顶端
E.plugin("RowMoveFirst", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.moveRow(eles.table, eles.row, 'first');
		return true;
	}
});

// 移动到最底端
E.plugin("RowMoveLast", {
	click: function( Menu, self, target ){
		var eles = Event.getElement();
		Table.moveRow(eles.table, eles.row, 'last');
		return true;
	}
});

// 表格属性
E.plugin("TableProps", {
	config: {
		position: 'center',
		//面板宽度
		width: 400
	},
	
	// 还有列跨度，行跨度
	init: function( self ){
		self.cache['html'] = '<table width="100%" border="0" class="jqe-plugin-tableprops">\
			<tbody><tr><td colspan="4" class="jqe-plugin-tableprops-title">\
			表格<span class="jqe-panel-notice">(设置表格属性)</td></tr>\
			<tr><td>行数:<br/><input type="text" name="talbeRows" maxlength="3"/></td>\
				<td>表格宽度:<br/><input type="text" name="talbeWidth" maxlength="5"/></td>\
				<td>表格对齐:<br/><select name="tableAlign"><option value="left">左对齐</option>\
				<option value="center">居中</option>\
				<option value="right">右对齐</option></select></td></tr>\
			<tr><td>列数:<br/><input type="text" name="talbeCols" maxlength="3"/></td>\
			<td>表格高度:<br/><input type="text" name="talbeHeight" maxlength="5"/></td>\
			<td>表格边框:<br/><input type="text" name="talbeBorder" maxlength="2"/></td></tr>\
			<tr><td colspan="4" class="jqe-plugin-tableprops-title">\
			行<span class="jqe-panel-notice">(设置整行单元格的属性)</span></td></tr>\
			<tr><td>单元格背景:<br/><input type="text" name="rowBg" maxlength="16"/></td>\
			<td>单元格宽度:<br/><input type="text" name="rowWidth" maxlength="3"/></td>\
			<td>单元格高度:<br/><input type="text" name="rowHeight" maxlength="3"/></td>\
			<td>单元格对齐:<br/><select name="tableAlign"><option value="left">左对齐</option>\
				<option value="center">居中</option>\
				<option value="right">右对齐</option></select></td></tr>\
			<tr><td colspan="4" class="jqe-plugin-tableprops-title">\
			列<span class="jqe-panel-notice">(设置整列单元格的属性)</span></td></tr>\
			<tr><td>单元格背景:<br/><input type="text" name="columnBg" maxlength="16"/></td>\
			<td>单元格宽度:<br/><input type="text" name="columnWeidth" maxlength="3"/></td>\
			<td>单元格高度:<br/><input type="text" name="columnHeight" maxlength="3"/></td>\
			<td>单元格对齐:<br/><select name="tableAlign"><option value="left">左对齐</option>\
				<option value="center">居中</option>\
				<option value="right">右对齐</option></select></td></tr>\
			<tr><td colspan="4" class="jqe-plugin-tableprops-title">\
			单元格<span class="jqe-panel-notice">(仅设置当前一个单元格的属性)</span></td></tr>\
			<tr><td>单元格背景:<br/><input type="text" name="cellBg" maxlength="16"/></td>\
			<td>单元格宽度:<br/><input type="text" name="cellWidth" maxlength="3"/></td>\
			<td>单元格高度:<br/><input type="text" name="cellHeight" maxlength="3"/></td>\
			<td>单元格对齐:<br/><select name="tableAlign"><option value="left">左对齐</option>\
				<option value="center">居中</option>\
				<option value="right">右对齐</option></select></td></tr>\
			</tbody></table>';
	},
	
	callback: function(){
		
	},
	
	ok: function(panel, self){
		var bool = 0;
		Table.setTable( Menu );
		return bool;
	}
});

})(jQEditor, jQuery);